import 'package:flutter/material.dart';
import "package:get/get.dart";
import 'package:pull_to_refresh/pull_to_refresh.dart';

import '../../../common/my_icon.dart';
import '../../../common/style.dart';
import '../../../components/appbar_widget/appbar_widget.dart';
import '../../../components/loading/svg_loading.dart';
import '../../../components/status/empty.dart';
import '../../../components/status/load_more_state.dart';
import '../../../components/status/not_network.dart';
import '../../../config/global_config.dart';
import '../../../controller/network/network_ctrl.dart';
import '../../../model/bean/community_bean.dart';
import '../../../utils/screenutil/fit.dart';
import '../../../utils/tools/Tools.dart';
import 'ctrl/plan_ctrl.dart';

class Plan extends StatelessWidget {
  static const _WAITING = "待维保";
  static const _COMPLETE = "已维保";
  static const _END = "未维保";

  // 导入控制器
  final planCtrl = Get.put(PlanCtrl());
  final _nc = Get.find<NetworkCtrl>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBarWidget('维保计划').build(context),
      body: Obx(
        () => planCtrl.isInitComplete.value
            ? Container(
                height: double.infinity,
                child: Stack(
                  children: [
                    Container(
                      margin: EdgeInsets.only(
                          top: fit(_nc.topDistance.value + 150)),
                      child: planCtrl.MARKSYMBOL.value
                          ? SmartRefresher(
                              footer: CustomFooter(
                                builder: (BuildContext ct, LoadStatus mode) {
                                  Widget body = LoadMoreState(mode);
                                  return Container(child: Center(child: body));
                                },
                              ),
                              enablePullDown: false,
                              enablePullUp: true,
                              controller: planCtrl.refreshController.value,
                              onLoading: planCtrl.onLoading,
                              child: ListView.builder(
                                itemCount: planCtrl.listData.length,
                                itemBuilder: (BuildContext c, int i) => InkWell(
                                  onTap: () => planCtrl
                                      .onLeave(planCtrl.listData[i].elevatorId),
                                  child: Container(
                                    color: Colors.white,
                                    child: Column(
                                      crossAxisAlignment:
                                          CrossAxisAlignment.end,
                                      children: [
                                        Container(
                                          width: 640.f,
                                          padding: EdgeInsets.only(
                                            left: 20.f,
                                            top: 15.f,
                                            bottom: 15.f,
                                          ),
                                          child: Container(
                                            child: Row(
                                              mainAxisAlignment:
                                                  MainAxisAlignment
                                                      .spaceBetween,
                                              children: [
                                                Container(
                                                  child: Column(
                                                    crossAxisAlignment:
                                                        CrossAxisAlignment
                                                            .start,
                                                    mainAxisAlignment:
                                                        MainAxisAlignment
                                                            .center,
                                                    children: [
                                                      Container(
                                                        width: 420.f,
                                                        margin: EdgeInsets.only(
                                                          bottom: 10.f,
                                                        ),
                                                        alignment: Alignment
                                                            .centerLeft,
                                                        child: Text(
                                                          "${planCtrl.listData[i].villageName}",
                                                          softWrap: true,
                                                          textAlign:
                                                              TextAlign.right,
                                                          overflow: TextOverflow
                                                              .ellipsis,
                                                          style: TextStyle(
                                                            fontSize: 24.f,
                                                          ),
                                                        ),
                                                      ),
                                                      Container(
                                                        child: Text(
                                                          "${planCtrl.listData[i].buildingNo}/${planCtrl.listData[i].unitNo}/${planCtrl.listData[i].shaft}",
                                                          style: TextStyle(
                                                            color: GlobalConfig
                                                                .titleContent,
                                                            fontSize: 22.f,
                                                          ),
                                                        ),
                                                      ),
                                                    ],
                                                  ),
                                                ),
                                                Container(
                                                  width: 140.f,
                                                  child: Text(
                                                    "${Tools.dateFormat(
                                                      DateTime.parse(planCtrl
                                                          .listData[i]
                                                          .maintenancePlanDate),
                                                      DataFormats.y_mo_d,
                                                    )}",
                                                    textAlign: TextAlign.right,
                                                    style: TextStyle(
                                                      fontSize: 22.f,
                                                      color: GlobalConfig
                                                          .assistColor,
                                                    ),
                                                  ),
                                                ),
                                                Container(
                                                  width: 40.f,
                                                  alignment:
                                                      Alignment.centerRight,
                                                  child: Icon(
                                                    Icons.chevron_right,
                                                    size: 35.f,
                                                    color: GlobalConfig
                                                        .assistColor,
                                                  ),
                                                )
                                              ],
                                            ),
                                          ),
                                        ),
                                        SizedBox(
                                          width:
                                              planCtrl.listData.length - 1 == i
                                                  ? 640.f
                                                  : 620.f,
                                          height: 1.f,
                                          child: Container(
                                            color: GlobalConfig.borderColor,
                                          ),
                                        ),
                                      ],
                                    ),
                                  ),
                                ),
                              ),
                            )
                          : Container(
                              width: 640.f,
                              height: double.infinity,
                              child: Center(child: Empty()),
                            ),
                    ),

                    // 选项卡、条件选择器
                    Positioned(
                      child: Container(
                        width: 640.f,
                        height: 150.f,
                        margin: EdgeInsets.only(
                          top: fit(_nc.topDistance.value),
                        ),
                        decoration: BoxDecoration(
                          boxShadow: GlobalConfig.boxShadow,
                        ),
                        child: Column(
                          children: [
                            Container(
                              width: 640.f,
                              height: 85.f,
                              color: Colors.white,
                              child: Flex(
                                direction: Axis.horizontal,
                                children: [
                                  Expanded(
                                    flex: 1,
                                    child: createTabs(_WAITING, 0),
                                  ),
                                  SizedBox(
                                    width: 1.f,
                                    child: Container(
                                      height: 60.f,
                                      decoration: BoxDecoration(
                                        gradient: Style.gradient,
                                      ),
                                    ),
                                  ),
                                  Expanded(
                                    flex: 1,
                                    child: createTabs(_COMPLETE, 1),
                                  ),
                                  SizedBox(
                                    width: 1.f,
                                    child: Container(
                                      height: 60.f,
                                      decoration: BoxDecoration(
                                        gradient: Style.gradient,
                                      ),
                                    ),
                                  ),
                                  Expanded(
                                    flex: 1,
                                    child: createTabs(_END, 2),
                                  ),
                                ],
                              ),
                            ),
                            Container(
                              width: 640.f,
                              height: 64.f,
                              margin: EdgeInsets.only(top: 1.f),
                              color: Colors.white,
                              child: Flex(
                                direction: Axis.horizontal,
                                children: [
                                  Container(
                                    child: InkWell(
                                      onTap: () async {
                                        planCtrl.getDateRange(context);
                                      },
                                      child: Row(
                                        mainAxisAlignment:
                                            MainAxisAlignment.spaceBetween,
                                        children: [
                                          Container(
                                            alignment: Alignment.centerLeft,
                                            padding:
                                                EdgeInsets.only(left: 15.f),
                                            width: 275.f,
                                            child: Text(
                                              planCtrl.selectedDateRange.value,
                                              softWrap: true,
                                              maxLines: 1,
                                              textAlign: TextAlign.left,
                                              overflow: TextOverflow.ellipsis,
                                              style: TextStyle(
                                                color: planCtrl
                                                            .selectedDateRange
                                                            .value ==
                                                        "请选择日期"
                                                    ? GlobalConfig.assistColor
                                                    : Colors.black,
                                                fontSize: 22.f,
                                              ),
                                            ),
                                          ),
                                          // Icons.chevron_right
                                          InkWell(
                                            onTap: () =>
                                                planCtrl.emptyDateRange(),
                                            child: Container(
                                              width: 64.f,
                                              height: 64.f,
                                              child: Icon(
                                                planCtrl.selectedDateRange
                                                            .value ==
                                                        "请选择日期"
                                                    ? Icons.chevron_right
                                                    : MyIcon.cuowu,
                                                color: GlobalConfig.assistColor,
                                                size: 32.f,
                                              ),
                                            ),
                                          ),
                                        ],
                                      ),
                                    ),
                                  ),
                                  SizedBox(
                                    width: 1.f,
                                    child: Container(
                                      height: 60.f,
                                      color: GlobalConfig.borderColor,
                                    ),
                                  ),
                                  Expanded(
                                    flex: 1,
                                    child: InkWell(
                                      onTap: () async {},
                                      child: Row(
                                        mainAxisAlignment:
                                            MainAxisAlignment.spaceBetween,
                                        children: [
                                          Container(
                                            width: 300.f,
                                            padding: EdgeInsets.only(
                                              left: 20.f,
                                            ),
                                            child: DropdownButtonHideUnderline(
                                              child: DropdownButton(
                                                items: planCtrl
                                                    .communityData.value,
                                                value: planCtrl
                                                    .selectedCommunity.value,
                                                hint: Container(
                                                  width: 240.f,
                                                  child: Text(
                                                    "${planCtrl.selectedCommunity.value != null ? planCtrl.selectedCommunity.value.name : '请选择小区'}",
                                                    softWrap: true,
                                                    maxLines: 1,
                                                    textAlign: TextAlign.left,
                                                    overflow:
                                                        TextOverflow.ellipsis,
                                                    style: TextStyle(
                                                      color: GlobalConfig
                                                          .assistColor,
                                                      fontSize: 22.f,
                                                    ),
                                                  ),
                                                ),
                                                icon: Icon(
                                                  Icons.chevron_right,
                                                  color:
                                                      GlobalConfig.assistColor,
                                                  size: 35.f,
                                                ),
                                                onChanged: (CommunityBean
                                                        val) =>
                                                    planCtrl
                                                        .setDropDownSelectedData(
                                                            val),
                                              ),
                                            ),
                                          ),
                                        ],
                                      ),
                                    ),
                                  ),
                                ],
                              ),
                            )
                          ],
                        ),
                      ),
                    ),

                    // 网络状态
                    Positioned(top: 0, child: NotNetwork()),
                  ],
                ),
              )
            : SVGLoading(),
      ),
    );
  }

  /// 创建头部选项卡
  /// * [name] String 选项卡名称|必填
  /// * [currentStatus] MaintenanceStatus 当前状态|必填
  Widget createTabs(String name, int currentStatus) {
    return InkWell(
      onTap: () => planCtrl.onChangeSelectStatus(currentStatus),
      child: Container(
        alignment: Alignment.center,
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Container(
              width: 140.f,
              height: 85.f,
              alignment: Alignment.center,
              decoration: BoxDecoration(
                border: Border(
                  bottom: BorderSide(
                    width: 2,
                    color: planCtrl.status.value == currentStatus
                        ? GlobalConfig.theme
                        : Colors.white,
                  ),
                ),
              ),
              child: Text(
                "$name",
                style: TextStyle(
                  fontSize: 26.f,
                  color: planCtrl.status.value == currentStatus
                      ? GlobalConfig.theme
                      : GlobalConfig.assistColor,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
